﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{


    /// <summary>
    /// 所有井的集合
    /// </summary>
    [Serializable]
    public class WellCollection : BoundaryCollecionBase
    {

        public WellCollection(int nrows, int ncolumns, int nlayers)
            : base(nrows, ncolumns, nlayers)
        {

            _innerList = new List<Well>();


        }


        List<Well> _innerList;

        public List<Well> InnerList
        {
            get { return _innerList; }

        }





        #region 公共函数

        public void AddWell(Well well)
        {
            this._innerList.Add(well);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(well, SCCollectionChangedType.ADD, SCType.WELL));



        }

        public void RemoveWell(Well well)
        {
            this._innerList.Remove(well);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(well, SCCollectionChangedType.REMOVE, SCType.WELL));
        }


        public void ResetWells(List<Well> Wells)
        {
            this._innerList.Clear();

            this._innerList.AddRange(Wells);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.WELL));


        }
        public void ModifyWell(Well well)
        {
            foreach (Well item in InnerList)
            {
                if (item.Index == well.Index)
                {
                    int location = InnerList.IndexOf(item);
                    InnerList.RemoveAt(location);
                    InnerList.Insert(location, well);
                }
            }
            this.AskCollectionChanged(new SCColleconChangedEventArgs(well, SCCollectionChangedType.MODIFY, SCType.WELL));
        }

        public List<Well> GetWell(int i, int j, int k)
        {
            List<Well> wellList = new List<Well>();
            foreach (Well well in InnerList)
            {
                if (well.I == i && well.J == j && well.K == k)
                {
                    wellList.Add(well);
                }
            }
            return wellList;

        }

        public List<Well> GetWell(int index)
        {
            List<Well> wellList = new List<Well>();
            foreach (Well well in InnerList)
            {
                if (well.Index == index)
                {
                    wellList.Add(well);
                }
            }
            return wellList;
        }

        public void SetWells(List<Well> wells)
        {
            int i = 0;
            foreach (Well well in wells)
            {
                this._innerList[i].I = well.I;
                this._innerList[i].J = well.J;
                this._innerList[i].K = well.K;
                this._innerList[i].Index = well.Index;
                this._innerList[i].Name = well.Name;
                this._innerList[i].RechargeRates = well.RechargeRates;

                i++;
            }
            this.AskCollectionChanged(new SCColleconChangedEventArgs(wells, SCCollectionChangedType.MODIFY, SCType.WELL));
        }


        /// <summary>
        /// 跟新指定index的源汇条件
        /// </summary>
        /// <param name="rivers"></param>
        /// <param name="indices"></param>
        public void UpdateWells(IList<Well> wells, List<int> indices)
        {

            List<Well> orgHeads = new List<Well>();

            foreach (int index in indices)
            {
                orgHeads.AddRange(this.GetWell(index));

            }


            foreach (Well head in orgHeads)
            {
                this._innerList.Remove(head);


            }


            this._innerList.AddRange(wells);


            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.WELL));



        }



        override public void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
        {
            base._stressPeriods_StressPeriodChanged(sender, e);

            List<StressPeriod> lst = (List<StressPeriod>)e.Param;
            foreach (Well item in this._innerList)
            {
                item.OnResetSperiods(lst.Count);
            }

        }

        public PWell ToWellPack()
        {
            PWell pw = new PWell();
            pw.MXACTW = this._innerList.Count;



            //pw.IWELCB = 0;

            for (int i = 0; i < this.NPeriods; i++)
            {
                SPWell sp = new SPWell();


                if (i == 0)
                {
                    sp.ITMP = this._innerList.Count;

                    foreach (Well item in this._innerList)
                    {
                        GPointValue pv = new GPointValue(item.K, item.I, item.J, item.RechargeRates[i]);


                        sp.WellData.Add(pv);
                    }


                }

                else
                {
                    if (this.IsUsePrecious[i] == true)
                    {
                        sp.ITMP = -1;

                    }
                    else
                    {

                        sp.ITMP = this._innerList.Count;

                        foreach (Well item in this._innerList)
                        {
                            GPointValue pv = new GPointValue(item.K, item.I, item.J, item.RechargeRates[i]);


                            sp.WellData.Add(pv);
                        }

                    }
                }


               pw.StressPeriodWell.Add(sp);


                
            }
             return pw;
        }


        public void Clear()
        {
            this._innerList.Clear();

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.WELL));

 
        }

        public override void OnGridReset(int nrows, int ncols, int nlayers)
        {

            this.Clear();

            base.OnGridReset(nrows, ncols, nlayers);

        }

        #endregion

        List<Well> _tempWells = new List<Well>();
        bool _haveTempData = false;
        public bool HaveTempData
        {
            get { return _haveTempData; }
        }

        public void AddTempWells(List<Well> well)
        {
            this._tempWells.AddRange(well);
            this._haveTempData = true;
        }


        /// <summary>
        /// 将暂存的数据提供给核心，并清空对应的选项
        /// </summary>
        public void CommitTempWells()
        {
            if (this._haveTempData)
            {
                this.ResetWells(this._tempWells);
                this._tempWells.Clear();
            }
            this._haveTempData = false;
        }
    }
}
